/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import data.DataProvider;
import entities.Artwork;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author bertram
 */
public class Works extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        String uri = request.getRequestURI();
        DataProvider prov = new DataProvider();
        PrintWriter out = response.getWriter();
        try {
            System.out.println(uri);
            String action = request.getRequestURI().split("works/")[1];
            if (uri.endsWith("works/")) {
            }//gør noget så radikalt som at spytte en liste tilbage
            else if (uri.toLowerCase().contains("tags")) {
                System.out.println("der er tags");
                try {

                    String tag = request.getParameter("tag");
                    JSONArray retArr = new JSONArray();
                    for (Artwork w : prov.getWorksByTag(tag)) {
                        retArr.put(w.toJSON());
                    }
                    JSONObject obj = new JSONObject();
                    obj.put("billeder", retArr);
                    obj.put("tag", tag);
                    out.write(obj.toString());
                    System.out.println(obj.toString());

                } catch (JSONException jsex) {
                    System.out.println(jsex.getCause());
                }

            } else if (uri.toLowerCase().contains("museum")) {

            } else {
                int workID = Integer.parseInt(uri.split("works/")[1]);
                Artwork work = prov.getWork(workID);
                //work.addArtist(prov.);
                try {
                    JSONObject obj = work.toJSON();
                    obj.put("type", work.getType());
                    out.write(obj.toString());
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }

            }
        } finally {
            out.close();


        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";

    }// </editor-fold>
}
